<?php
/**
 * Created by PhpStorm.
 * User: zhangrxiang
 * Date: 2016/12/21
 * Time: 13:38
 */

function &test() {
	static $b = 0;//申明一个静态变量
	$b = $b + 1;
	echo $b.PHP_EOL;
	return $b;
}

$a = test();//这条语句会输出　$b的值　为１
$a = 5;
$a = test();//这条语句会输出　$b的值　为2
$a = & test();//这条语句会输出　$b的值　为3
$a = 5;
$a = test();//这条语句会输出　$b的值　为6

$array1 = array(1,2);
$x = &$array1[1];   // Unused reference
//$x = 12;
unset($x);
//print_r($array1);
$array2 = $array1;  // reference now also applies to $array2 !
$array2[1]=22;      // (changing [0] will not affect $array1)
print_r($array1);
print_r($array2);

$arr = array('a'=>'first', 'b'=>'second', 'c'=>'third');
foreach ($arr as &$a){
	echo $a.PHP_EOL;
	unset($a);
}; // do nothing. maybe?
print_r($arr);
foreach ($arr as $a){
	echo $a.PHP_EOL;
};  // do nothing. maybe?
print_r($arr);

function foo(&$var) { }

foo($a); // $a is "created" and assigned to null

$b = array();
foo($b['b']);
var_dump(array_key_exists('b', $b)); // bool(true)

$c = new StdClass;
foo($c->d);
var_dump(property_exists($c, 'd')); // bool(true)


// Example one
$arr1 = array(1);
echo "\nbefore:\n";
echo "\$arr1[0] == {$arr1[0]}\n";
$arr2 = $arr1;
$arr2[0]++;
echo "\nafter:\n";
echo "\$arr1[0] == {$arr1[0]}\n";
echo "\$arr2[0] == {$arr2[0]}\n";

// Example two
$arr3 = array(1);
$a =& $arr3[0];
echo "\nbefore:\n";
echo "\$a == $a\n";
echo "\$arr3[0] == {$arr3[0]}\n";
unset($a);
$arr4 = $arr3;
$arr4[0]++;
echo "\nafter:\n";
//echo "\$a == $a\n";
echo "\$arr3[0] == {$arr3[0]}\n";
echo "\$arr4[0] == {$arr4[0]}\n";